﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Base;
using Base.Core;
using AonesoftBase.Biz;
using AonesoftBase;
using System.IO;
public partial class CreateCompany_sitemap : Base.BasePage
{
    // Methods
    private string bind(int id)
    {
        int num = 0xc350;
        int num2 = 1;
        StringBuilder builder = new StringBuilder();
        string strSelect = " top " + num + " LoginName ,CompanyName";
        string str2 = string.Format(" where 1=1 and verify='1' and  userid between {0} and {1} AND (LEN(CAST(Summary AS varchar)) > 5 or gradeid=2) ", (id - 1) * 0xc350, id * 0xc350);
        string strOrder = " order by RegDate desc";
        DataTable table = nspLable.Function.Function.GetDataTable(strSelect, str2.ToString(), "U_UserInfo", strOrder);
        if (table.Rows.Count <= 0)
        {
            return "还没有会员！";
        }
        string webURL = new WebConfig().WebURL;
        int count = table.Rows.Count;
        num = count;
        int num4 = num2;
        int num5 = ((count % num4) == 0) ? (count / num4) : ((count / num4) + 1);
        builder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">");
        for (int i = 0; i < num5; i++)
        {
            for (int j = 0; j < num4; j++)
            {
                builder.Append("<url>\n ");
                int num8 = (i * num4) + j;
                if ((num8 > (count - 1)) || (num8 > num))
                {
                    break;
                }
                builder.Append("<loc>");
                builder.Append(webURL + "shop/index/" + table.Rows[num8]["LoginName"].ToString() + BasePage.Config.Rewrite + "</loc>\n");
                builder.Append("<priority>0.5</priority>\n ");
                builder.Append("<changefreq>daily</changefreq>\n ");
                builder.Append("</url>\n ");
            }
        }
        builder.Append("</urlset>");
        return builder.ToString();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        BasePage.CheckRights("column", "A1");
        if (!base.IsPostBack)
        {
            BaseCommon common = new BaseCommon(DBcommon.DBConn);
            decimal d = decimal.Parse(common.GetMax("UserId", "U_UserInfo").ToString()) / 50000M;
            int num3 = int.Parse(Math.Ceiling(d).ToString());
            for (int i = 1; i <= num3; i++)
            {
                string filename = string.Format("Com{0}-{1}", i - 1, i);
                WriteFile(this.bind(i), filename);
            }
            base.Response.Write("会员SiteMap生成成功！&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:history.go(-1);'>返回</a>");
        }
    }

    public static bool WriteFile(string str, string filename)
    {
        string path = AppDomain.CurrentDomain.BaseDirectory.ToString() + @"sitemap\" + filename + ".xml";
        Encoding encoding = Encoding.GetEncoding("UTF-8");
        StreamWriter writer = null;
        try
        {
            writer = new StreamWriter(path, false, encoding);
            writer.Write(str);
            writer.Flush();
        }
        catch (Exception exception)
        {
            HttpContext.Current.Response.Write(exception.Message);
            HttpContext.Current.Response.End();
        }
        finally
        {
            writer.Close();
        }
        return true;
    }
}